
package com.shop.cloud.weixin.admin.controller;

import com.shop.cloud.common.core.util.R;
import com.shop.cloud.weixin.admin.config.open.WxOpenConfiguration;
import com.shop.cloud.weixin.common.constant.WxReturnCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;

/**
 * 微信账号配置
 *
 * @author
 * @date 2019-03-23 21:26:35
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxmasummary")
@Tag(description = "wxmasummary", name = "微信小程序数据分析接口")
public class WxMaSummaryController {

	/**
	 * 获取用户访问小程序数据概况
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "获取用户访问小程序数据概况")
	@GetMapping("/dailysummary")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getDailySummary(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getDailySummaryTrend(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("获取用户访问小程序数据概况失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("获取用户访问小程序数据概况失败",e);
			return R.failed("获取用户访问小程序数据概况失败");
		}
	}

	/**
	 * 获取日访问趋势
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "获取日访问趋势")
	@GetMapping("/dailyvisittrend")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getDailyVisitTrend(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getDailyVisitTrend(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("获取日访问趋势失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("获取日访问趋势失败",e);
			return R.failed("获取日访问趋势失败");
		}
	}

	/**
	 * 获取周访问趋势
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "获取周访问趋势")
	@GetMapping("/weeklyvisittrend")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getWeeklyVisitTrend(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getWeeklyVisitTrend(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("获取周访问趋势失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("获取周访问趋势失败",e);
			return R.failed("获取周访问趋势失败");
		}
	}

	/**
	 * 获取月访问趋势
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "获取月访问趋势")
	@GetMapping("/monthlyvisittrend")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getMonthlyVisitTrend(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getMonthlyVisitTrend(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("获取月访问趋势失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("获取月访问趋势失败",e);
			return R.failed("获取月访问趋势失败");
		}
	}

	/**
	 * 获取访问分布
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "获取访问分布")
	@GetMapping("/visitdistribution")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getVisitDistribution(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getVisitDistribution(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("获取访问分布失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("获取访问分布失败",e);
			return R.failed("获取访问分布失败");
		}
	}

	/**
	 * 日留存
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "日留存")
	@GetMapping("/dailyretaininfo")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getDailyRetainInfo(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getDailyRetainInfo(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("日留存失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("日留存失败",e);
			return R.failed("日留存失败");
		}
	}

	/**
	 * 周留存
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "周留存")
	@GetMapping("/weeklyretaininfo")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getWeeklyRetainInfo(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getWeeklyRetainInfo(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("周留存失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("周留存失败",e);
			return R.failed("周留存失败");
		}
	}

	/**
	 * 月留存
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "月留存")
	@GetMapping("/monthlyretaininfo")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getMonthlyRetainInfo(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getMonthlyRetainInfo(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("月留存失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("月留存失败",e);
			return R.failed("月留存失败");
		}
	}

	/**
	 * 获取访问页面数据
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "获取访问页面数据")
	@GetMapping("/visitpage")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getVisitPage(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getVisitPage(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("获取访问页面数据失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("获取访问页面数据失败",e);
			return R.failed("获取访问页面数据失败");
		}
	}

	/**
	 * 获取小程序新增或活跃用户的画像分布数据
	 * @param appId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Operation(summary = "获取小程序新增或活跃用户的画像分布数据")
	@GetMapping("/userportrait")
	@PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
	public R getUserPortrait(String appId, String startDate, String endDate) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			return R.ok(WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getAnalysisService()
					.getUserPortrait(sdf.parse(startDate), sdf.parse(endDate)));
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("获取小程序新增或活跃用户的画像分布数据失败",e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("获取小程序新增或活跃用户的画像分布数据失败",e);
			return R.failed("获取小程序新增或活跃用户的画像分布数据失败");
		}
	}
}
